using System;

using System.Configuration;

namespace TimelineNet.Library {
    [Serializable()]
    public class Timeline : System.Collections.Generic.List<Band> {

        public static System.Collections.Generic.Dictionary<string, string> GroupedEventColors = new System.Collections.Generic.Dictionary<string, string>();

        public static string[] GroupColors = new string[] { 
            System.Drawing.Color.AliceBlue.Name, System.Drawing.Color.Aqua.Name, 
            System.Drawing.Color.Azure.Name, System.Drawing.Color.Black.Name, 
            System.Drawing.Color.BlanchedAlmond.Name, System.Drawing.Color.Blue.Name, 
            System.Drawing.Color.Brown.Name, System.Drawing.Color.BurlyWood.Name, 
            System.Drawing.Color.CadetBlue.Name, System.Drawing.Color.Chartreuse.Name, 
            System.Drawing.Color.Chocolate.Name, System.Drawing.Color.Coral.Name, 
            System.Drawing.Color.Crimson.Name, System.Drawing.Color.DarkBlue.Name, 
            System.Drawing.Color.DarkGray.Name, System.Drawing.Color.DarkGreen.Name, 
            System.Drawing.Color.DarkKhaki.Name, System.Drawing.Color.DarkOrange.Name, 
            System.Drawing.Color.DarkSlateBlue.Name, System.Drawing.Color.DarkTurquoise.Name, 
            System.Drawing.Color.DeepPink.Name, System.Drawing.Color.DimGray.Name, 
            System.Drawing.Color.Firebrick.Name, System.Drawing.Color.ForestGreen.Name, 
            System.Drawing.Color.Yellow.Name, System.Drawing.Color.Gold.Name, 
            System.Drawing.Color.Gray.Name };
        public static System.Random rnd = new Random();


        public string ParentElementID;
        public string TimelineVarName;
        public System.Collections.Generic.List<EventSource> EventSources;
        public string XMLDocument;
        public Theme Theme;
        public bool Vertical = false;

        public Timeline() {
        }

        private TimelineData dataSource;

        public TimelineData DataSource {
            get { return dataSource;}
            set { dataSource = value; }
        }

        public int DayCountThreshold = 10;
        public int MonthBand = -1;

        public string RenderTimelines() {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            //emit evet sources
            if(EventSources != null) {
                foreach(EventSource s in EventSources) {
                    sb.Append(s.ToString());
                }
            }

            if(this.Theme != null) {
                sb.Append(Theme.ToString());
            }


            //emit bandinfos
            sb.Append("var bandInfos = [");
            int x = 0;
            foreach(Band t in this) {
                sb.Append(t.ToString());
                x++;
                if(x < this.Count) sb.Append(",");
            }
            sb.Append("];");

            //emit bandInfo synch bindings
            x = 0;
            foreach(Band t in this) {
                if (t.SynchWith >= 0) {
                    sb.Append("bandInfos[" + x + "].syncWith = " + t.SynchWith + ";\n");
                    if (t.Highlight) sb.Append("bandInfos[" + x + "].highlight = " + t.Highlight.ToString().ToLower() + ";\n");
                    //sb.Append("bandInfos[" + x + "].eventPainter.setLayout(bandInfos[" + t.SynchWith + "].eventPainter.getLayout());\n");
                }
                if (t.ThumbnailEventPainter != null) {
                    sb.Append("bandInfos[" + x + "]" + t.ThumbnailEventPainter.ToString());
                }
                x++;
            }

            //emit timeline create
            sb.Append(TimelineVarName);
            sb.Append("=Timeline.create(document.getElementById(\"" + ParentElementID + "\"), bandInfos"+(this.Vertical?",1":"")+");\n");

            //emit timeline xml document binding
            if(XMLDocument != null) {
                sb.Append("Timeline.loadXML(\"");
                sb.Append(XMLDocument);
                sb.Append("\", function(xml, url) { eventSource.loadXML(xml, url); });");
            }

            return sb.ToString();
        }
        public static string FormatDate(DateTime Date)
        {
            //string date = Date.ToString("MMM dd yyyy HH:mm:ss");
            string date = Date.ToString("R");
            if(date.Contains(" 00:00:00")) date = date.Replace(" 00:00:00", "");
            return date;
            //return Date.ToUniversalTime().ToString();
        }

        static int localOffset = -99;
        public static int LocalTimezoneOffSet
        {
            get
            {
                if(localOffset==-99) localOffset = TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now).Hours;
                return localOffset;
            }
        }
    }
}